<?php

class PlanningController extends Zend_Controller_Action
{

	public function init()
	{
		/* Initialize action controller here */
		$this->view->logged = Zend_Auth::getInstance()->hasIdentity();
		$this->view->idView = "planningView";
		
		if(!Zend_Auth::getInstance()->hasIdentity()) {
			$this->_redirect('index/index');
		}
		
		// A Décommenter si on veut utiliser la redirection sur SSL
		//Perso_ConfigSSL::config();
	}

	public function editprojectAction() 
	{		
		require_once APPLICATION_PATH . '/models/DbTable/Project.php';
		require_once APPLICATION_PATH . '/models/DbTable/Person.php';
		require_once APPLICATION_PATH . '/forms/EditprojectForm.php';
        
        // Retrieve the project name
		$projectName = $this->_getParam('project', '');
		
		if($projectName=="") {
			$this->_redirect('listproject/index');
		}
        
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
        $idPerson = $data->idPerson;
        $Person = new Person();
                        
		$form = new EditprojectForm();
		$this->view->form = $form;
		$this->view->projectName = $projectName;
		
		$Project = new Project();
		$idProject = $Project->getProjectId($projectName, $idPerson);

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			
		if ($form->isValid($formData)) {
				$data = $form->getValues();
				
				$data['enterprise'] = addslashes($data['enterprise']);
				$data['website'] = addslashes($data['website']);
				$data['startDate'] = addslashes($data['startDate']);
				$data['endDate'] = addslashes($data['endDate']);
				$data['note'] = addslashes($data['note']);

				$Project->setProjectInformations($idProject, $data['enterprise'], $data['website'], $data['startDate'], $data['endDate'], $data['note']);
				$this->_redirect('listproject/index');
			} else {
				$form->populate($formData);
			}

		} else {
			$DBdata = $Project->getProject($idProject);
			// Format the date from SQL to the good format. Empty if date = 0/0/0
			$DBdata['startDate'] = Perso_DateUtils::formatSQLDate('d-m-Y', $DBdata['startDate']);
			$DBdata['endDate'] = Perso_DateUtils::formatSQLDate('d-m-Y', $DBdata['endDate']);

			$form->populate($DBdata);
		}
		
		// Load form css
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		$this->view->headLink()->prependStylesheet($baseUrl.'/css/form.css');
	}

	public function showdiagrammAction()
	{
		// Add The requested JS library
		$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
		$this->view->headScript()->appendFile($baseUrl.'/js/jquery-1.3.2.js');
		$this->view->headScript()->appendFile($baseUrl.'/js/ui/jquery-ui-1.7.2.custom.js');
		$this->view->headScript()->appendFile($baseUrl.'/js/nestedSortables/interface-1.2.js');
		$this->view->headScript()->appendFile($baseUrl.'/js/nestedSortables/inestedsortable.js');
		$this->view->headScript()->appendFile($baseUrl.'/js/contextmenu/jquery.contextMenu.js');

		// Load css for the diagramm 
		$this->view->headLink()->prependStylesheet($baseUrl.'/css/jquery-ui-1.7.2.custom.css');
		$this->view->headLink()->prependStylesheet($baseUrl.'/css/jquery.contextMenu.css');	
		$this->view->headLink()->prependStylesheet($baseUrl.'/css/diagramm.css');
		
		// Load planning css
		$this->view->headLink()->prependStylesheet($baseUrl.'/css/planning.css');
	
		$nameProject = $this->_getParam('project', '');
		$this->view->projectName = $nameProject;

		if($nameProject=="") {
			$this->_redirect('listproject/index');
		}
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();

		require_once APPLICATION_PATH . '/models/DbTable/Project.php';
		$Project = new Project();

		$result = $Project->checkProjectOwner($nameProject,$data->idPerson);
		
		if(!$result) {
			$this->_redirect('listproject/index');
			
		} else {
			$result = $Project->getDiagramm($nameProject,$data->idPerson);
			if($result[0]['diagramm']!=''){
				$this->view->dia = $result[0]['diagramm'];
			}else{
				$this->view->dia = "vide";
			}	
			$this->view->id = $result[0]['idProject'];
			$this->view->isSaveEnable = true;
		}
	}
	
	public function saveplanningAction()
	{
		 if($this->getRequest()->isPost()){
		 
		 	// Cancel the magic quotes effect if enable on server
			//src: http://www.php.net/manual/en/security.magicquotes.disabling.php
		 	if (get_magic_quotes_gpc()) {
				$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
				while (list($key, $val) = each($process)) {
					foreach ($val as $k => $v) {
						unset($process[$key][$k]);
						if (is_array($v)) {
							$process[$key][stripslashes($k)] = $v;
							$process[] = &$process[$key][stripslashes($k)];
						} else {
							$process[$key][stripslashes($k)] = stripslashes($v);
						}
					}
				}
				unset($process);
			}
			
			$post = $this->getRequest()->getPost();
			require_once APPLICATION_PATH . '/models/DbTable/Project.php';
			$Project = new Project();
			$Project->setDiagramm($post['id'],($post['dia'])	);	
		}
	}
}


